import uuid
import json
from datetime import datetime, timedelta
import os

# 创建 data 目录（如果不存在）
data_dir = os.path.join(os.getcwd(), "data")
os.makedirs(data_dir, exist_ok=True)

###
(0, "恢复")
(1, "严重")
(2, "警告")
(3, "一般")
(4, "通知")
###
class AlertModel:
    def __init__(self, id, timestamp, source, level, business, content, host_ip, host, extend_map):
        self.id = id
        self.timestamp = timestamp
        self.source = source
        self.level = level
        self.business = business
        self.content = content
        self.host_ip = host_ip
        self.host = host
        self.extend_map = extend_map

    def to_dict(self):
        return {
            "id": self.id,
            "timestamp": self.timestamp.isoformat(),
            "source": self.source,
            "level": self.level,
            "business": self.business,
            "content": self.content,
            "host_ip": self.host_ip,
            "host": self.host,
            "extend_map": self.extend_map
        }

def now_datetime():
    return datetime.now()

def generate_uuid():
    return str(uuid.uuid4())

def generate_id():
    return str(uuid.uuid4().int)[:8]

def write_string(filepath, content):
    with open(filepath, 'w') as file:
        file.write(content)

def kafka_data(filepath, content):
    with open(filepath, 'a') as file:
        file.write(content + '\n')

def main():
    alert_list = []

    extend_map = {
        "extend1": now_datetime().isoformat(),
        "extend2": generate_uuid(),
        "extend3": generate_id()
    }

    source = "alert_mock_data"

    main_alert = AlertModel(
        id=generate_id(),
        timestamp=now_datetime() - timedelta(seconds=5),
        source=source,
        level="1",
        business="信用卡积分权益系统",
        content="oracle活动会话数增长告警",
        host_ip="21.99.116.170",
        host="NODE01",
        extend_map=extend_map
    )
    alert_list.append(main_alert)

    alert1 = AlertModel(
        id=generate_id(),
        timestamp=now_datetime() - timedelta(seconds=3),
        source=source,
        level="2",
        business="信用卡积分权益系统",
        content="cims, Metrics Process limit %isat 83.45，告警类型:WARNING",
        host_ip="21.99.116.2",
        host="NODE02",
        extend_map=extend_map
    )
    alert_list.append(alert1)

    alert2 = AlertModel(
        id=generate_id(),
        timestamp=now_datetime(),
        source=source,
        level="2",
        business="信用卡积分权益系统",
        content="服务器 23.93118.790 ping不通，请关注!最新值0，【Ping.系统管理员处理】",
        host_ip="21.99.116.1",
        host="NODE03",
        extend_map=extend_map
    )
    alert_list.append(alert2)

    alert3 = AlertModel(
        id=generate_id(),
        timestamp=now_datetime() - timedelta(seconds=2),
        source=source,
        level="1",
        business="信用卡积分权益系统",
        content="cims, The Data Guard status of cims is Error ORA-16198Timeout incurred on internalchannel during remote archival.",
        host_ip="21.99.116.3",
        host="NODE04",
        extend_map=extend_map
    )
    alert_list.append(alert3)

    data_map = {
        "data": [alert.to_dict() for alert in alert_list],
        "key": "7705ed3140ded161ecba27e2d3eaaf72"
    }

    filepath = os.path.join(data_dir, "alert_mock_api_data.json")
    if os.path.exists(filepath):
        os.remove(filepath)
    write_string(filepath, json.dumps(data_map, ensure_ascii=False, indent=4))


    filepath = os.path.join(data_dir, "alert_mock_kafka_data.json")
    if os.path.exists(filepath):
        os.remove(filepath)
    kafka_data(filepath, json.dumps(main_alert.to_dict(), ensure_ascii=False, indent=4))
    kafka_data(filepath, json.dumps(alert1.to_dict(), ensure_ascii=False, indent=4))
    kafka_data(filepath, json.dumps(alert2.to_dict(), ensure_ascii=False, indent=4))
    kafka_data(filepath, json.dumps(alert3.to_dict(), ensure_ascii=False, indent=4))

if __name__ == "__main__":
    main()